# This example modifies code from Hadley Wickham (https://gist.github.com/hadley/233134)# It also uses data from Nathan Yau's flowingdata site (http://flowingdata.com/)unemp<-read.csv("http://datasets.flowingdata.com/unemployment09.csv")names(unemp)<-c("id", "state_fips", "county_fips", "name", "year", "?", "?", "?", "rate")unemp$county<-tolower(gsub(" County, [A-Z]{2}", "", unemp$name))unemp$state<-gsub("^.*([A-Z]{2}).*$", "\\1", unemp$name)county_df<-map_data("county")names(county_df)<-c("long", "lat", "group", "order", "state_name", "county")county_df$state<-state.abb[match(county_df$state_name, tolower(state.name))]county_df$state_name<-NULLstate_df<-map_data("state")choropleth<-merge(county_df, unemp, by =c("state", "county"))choropleth<-choropleth[order(choropleth$order), ]choropleth$rate_d<-cut(choropleth$rate, breaks =c(seq(0, 10, by =2), 35))# provide a custom tooltip to plotly with the county name and actual ratechoropleth$text<-with(choropleth, paste0("County: ", name, "Rate: ", rate))p<-ggplot(choropleth, aes(long, lat, group =group))+geom_polygon(aes(fill =rate_d, text =text), colour =alpha("white", 1/2), size =0.2)+geom_polygon(data =state_df, colour ="white", fill =NA)+scale_fill_brewer(palette ="PuRd")+theme_void()# just show the text aesthetic in the tooltipggplotly(p, tooltip ="text")